package com.jrzh.mvc.controller.sys.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jrzh.framework.I18nHelper;
import com.jrzh.framework.annotation.NoPermission;
import com.jrzh.framework.annotation.RequiredPermission;
import com.jrzh.framework.base.controller.BaseAdminController;
import com.jrzh.framework.bean.JqGridTree;
import com.jrzh.framework.bean.ResultBean;
import com.jrzh.mvc.constants.SysActionConstants;
import com.jrzh.mvc.service.sys.manage.SysServiceManage;
import com.jrzh.mvc.view.sys.ConfigView;

@RestController(ConfigController.LOCATION +"/ConfigController")
@RequestMapping(ConfigController.LOCATION)
public class ConfigController extends BaseAdminController{
	public static final String LOCATION = "sys/admin/config";
	
	@Autowired
	SysServiceManage sysServiceManage;
	
	@PostMapping(value = "treegrid")
	@RequiredPermission(key =  SysActionConstants.INDEX)
	public JqGridTree treegrid() {
		JqGridTree tree = new JqGridTree();
		try {
			List<ConfigView> configList = sysServiceManage.configService.treegrid();
			tree.setRecords(configList.size());
			tree.setTotal(configList.size());
			tree.setRows(configList);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return tree;
	}
	
	@PostMapping(value = "add")
	@RequiredPermission(key =  SysActionConstants.ADD)
	public ResultBean add(ConfigView view) {
		ResultBean result = new ResultBean();
		try {
			sysServiceManage.configService.addByView(view, getSessionUser());
			result.setStatus(ResultBean.SUCCESS);
			result.setMsg(I18nHelper.getI18nByKey("message.addTo_success", getSessionUser()));
		}catch (Exception e) {
			log.error(e.getMessage());
			result.setMsg(e.getMessage());
		}
		return result;
	}
	
	
	@PostMapping(value = "load/{id}")
	@NoPermission
	public ResultBean load(@PathVariable("id") String id) {
		ResultBean result = new ResultBean();
		try {
			result.setStatus(ResultBean.SUCCESS);
			result.setObjs(new Object[]{sysServiceManage.configService.findViewById(id)});
		} catch (Exception e) {
			log.error(e.getMessage());
			result.setMsg(e.getMessage());
		}
		return result;
	}
	
	@PostMapping(value = "edit/{id}")
	@RequiredPermission(key =  SysActionConstants.EDIT)
	public ResultBean edit(@PathVariable("id") String id, ConfigView view) {
		ResultBean result = new ResultBean();
		try {
			view.setId(id);
			sysServiceManage.configService.editByView(view, getSessionUser());
			sysServiceManage.configService.refreshCache(view.getKey(), view.getValue());
			result.setStatus(ResultBean.SUCCESS);
			result.setMsg(I18nHelper.getI18nByKey("message.modify_success", getSessionUser()));
			result.setObjs(new Object[]{view.getKey(),view.getValue()});
		}catch (Exception ex) {
			log.error(ex.getMessage());
			result.setMsg(ex.getMessage());
		}
		return result;
	}

	@Override
	protected void setData() {
		
	}
}
